/**
 * @author:fangxiaohua
*  @邮箱：abc2710712@qq.com
*  @qq:1295168875
*   用户业务操作操作
*/
void(function(exports){
	//数据库操作
	var dbLib = require('../lib/dbLib');
	//工具类模块
	var tools = require('../lib/toolsLib');
	
	//意见反馈列表
	exports.view = function(req, res){
		var  sql=" SELECT * from t_settings ";
		dbLib.dbQuery(sql,[],function(err,result){
			res.send(result[0]);
			return;
		});
	};
	
	//平台基本配置修改
	exports.edit = function(req, res){
		var id=req.body.id;
		var name=req.body.name;
		var logo=req.body.logo;
		var desc=req.body.desc;
		var keyword=req.body.keyword;
		var companyName=req.body.companyName;
		var companyAddress=req.body.companyAddress;
		var companyTel=req.body.companyTel;
		var icp=req.body.icp;
		var countCode=req.body.countCode;
		var domain=req.body.domain;
		var params=[name,logo,desc,keyword,companyName,companyAddress,companyTel,icp,countCode,domain,id];
		var sql="update t_settings w set w.name=?,w.logo=?,w.desc=?,w.keyword=?,w.company_name=?,w.company_address=?,w.company_tel=?,w.icp=?,w.count_code=?,w.domain=? where w.id=1";
		dbLib.dbUpdate(sql,params,function(err,result){
			if(err){
				res.send("false");
				return;
			}
			res.send("true");
			return;
		});
	};
	
	//添加吐槽
	exports.add=function(req,res){
		var content=req.body.content;
		var contact=req.body.contact;
		var sql="insert into  t_feedback (content,contact,submit_time,status) values(?,?,now(),0)";
		var params=[content,contact];
		dbLib.dbAdd(sql,params,function(err,result){
			if(err){
				res.send("false");
				return;
			}
			res.send("true");
			return;
		});
	}
	
	//菜单列表
	exports.mlist = function(req, res){
		sql=["select distinct m.id, m.url ,m.name,m.description,m.logo ,m.parent_id,m.status ",
			 " from t_menu m inner join t_role_menu rm on m.id=rm.menu_id ",
			 " where 1=1  and rm.role_id in (select distinct ur.role_id ",
			 " from t_user_role ur where ur.usr_id=?)  order by m.sort_index asc, m.id"].join("");
		var usrId=req.session.user.id;
		dbLib.dbQuery(sql,[usrId],function(err,result){
			res.send(result);
			return;
		});
	};
	
	
	//菜单列表
	exports.msort = function(req, res){
		sql="update t_menu m set m.sort_index=? where m.id=?";
		var ids=JSON.parse(req.body.data);
		for(var i in ids){
			dbLib.dbUpdate(sql,[i,ids[i].id]);
		}
		res.send("true");
		return;
	};
	
	//菜单修改
	exports.medit = function(req,res){
		var name = req.body.name;
		var logo = req.body.logo;
		var description=req.body.description;
		var url = req.body.url;
		var id = req.body.id;
		var sql="update t_menu m set m.`name`=? ,m.logo=?,m.description=?,m.url=? where m.id=?";
		var params=[name,logo,description,url,id];
		dbLib.dbUpdate(sql,params,function(err,result){
			if(err){
				res.send("false");
				return;
			}
			res.send("true");
			return;
		});
	}
	
	//菜单
	exports.mstatus = function(req, res){
		var status=req.body.status;
		var id=req.body.id;
		sql="update t_menu m set m.status=? where m.id=?";
		dbLib.dbUpdate(sql,[status,id],function(err,result){
				if(err){
					res.send("false");
					return;
				}
				res.send("true");
				return;
		});
	};
	
	
})( (function(){
    if(typeof exports === 'undefined') {
        window.exports = {};
        return window.exports;
    } else {
        return exports;
    }
})());